package cn.summit.tools;


import org.springframework.util.Assert;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

/**
 * 日期工具类
 *
 * @Author zj * @Date: 2018/12/19 13:38 * @Version 1.0
 */
public class DateUtils {

    private static final ZoneOffset ZONE = ZoneOffset.of("+8");
    /**
     * yyyyMMdd
     */
    public static final DateTimeFormatter DATE_SIMPLE_FORMATTER = DateTimeFormatter
        .ofPattern("yyyyMMdd");
    /**
     * yyyyMMdd
     */
    public static final DateTimeFormatter DATE_yyyyMM_FORMATTER = DateTimeFormatter
            .ofPattern("yyyyMM");

    /**
     * yyyy-MM-dd
     */
    public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE;
    /**
     * yyyy年MM月dd日
     */
    public static final DateTimeFormatter DATE_CN_FORMATTER = DateTimeFormatter
        .ofPattern("yyyy年MM月dd日");
    /**
     * MM月dd日
     */
    public static final DateTimeFormatter DATE_MD_FORMATTER = DateTimeFormatter.ofPattern("MM月dd日");

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter
        .ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * yyyy年MM月dd日 HH时mm分ss秒
     */
    public static final DateTimeFormatter DATE_TIME_CN_FORMATTER = DateTimeFormatter
        .ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
    /**
     * HH:mm:ss
     */
    public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");


    /**
     * 获取某季度的结束日期
     *
     * @param offset 0本季度，1下个季度，-1上个季度，依次类推
     * @return
     */
    public static LocalDate getQuarterEndDay(int offset) {
        final LocalDate date = LocalDate.now().plusMonths(offset * 3);
        //当月
        int month = date.getMonth().getValue();
        int end = 0;
        if (month <= 3) {
            end = 3;
        } else if (month >= 4 && month <= 6) {
            end = 6;
        } else if (month >= 7 && month <= 9) {
            end = 9;
        } else if ((month >= 10 && month <= 12)) {
            end = 12;
        }

        return date.plusMonths(end - month).with(TemporalAdjusters.lastDayOfMonth());
    }

    /**
     * 获取当前季末日期
     *
     * @return
     */
    public static LocalDate getQuarterEndDay() {
        return getQuarterEndDay(0);
    }


    /**
     * 计算时长(秒)
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static long getDurationSecond(LocalDateTime startTime, LocalDateTime endTime) {
        Assert.notNull(startTime, "开始时间不能为空");
        Assert.notNull(endTime, "结束时间不能为空");
        Duration duration = Duration.between(startTime, endTime);
        return duration.getSeconds();
    }


    /**
     * 获取指定时间秒数
     *
     * @param dateTime
     * @return
     */
    public static long toEpochSecond(LocalDateTime dateTime) {
        return dateTime.toEpochSecond(ZONE);
    }

    /**
     * 获取指定时间毫秒数
     *
     * @param dateTime
     * @return
     */
    public static Long toEpochMilli(LocalDateTime dateTime) {
        if (dateTime == null) {
            return null;
        }
        return dateTime.toInstant(ZONE).toEpochMilli();
    }


    /**
     * 获取周几
     *
     * @param week
     * @return
     */
    public static String getWeek(DayOfWeek week) {
        if (DayOfWeek.MONDAY == week) {
            return "周一";
        } else if (DayOfWeek.TUESDAY == week) {
            return "周二";
        } else if (DayOfWeek.WEDNESDAY == week) {
            return "周三";
        } else if (DayOfWeek.THURSDAY == week) {
            return "周四";
        } else if (DayOfWeek.FRIDAY == week) {
            return "周五";
        } else if (DayOfWeek.SATURDAY == week) {
            return "周六";
        } else if (DayOfWeek.SUNDAY == week) {
            return "周日";
        }
        return null;
    }


}
